package com.adopapa.nezhacloud.register.conf;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

	public static final String EXCHANGE = "nezhacloud.register.exchange";
	public static final String ROUTINGKEY = "nezhacloud.register.routingkey";
	public static final String QUEUE = "nezhacloud.register.queue";

	/**
	 * FanoutExchange 将消息分发到所有的绑定队列，无routingkey的概念 HeadersExchange
	 * 通过添加属性key-value匹配  DirectExchange 按照routingkey分发到指定队列  TopicExchange 多关键字匹配
	 */
	@Bean
	public DirectExchange defaultExchange() {
		// 交换不自动删除
		return new DirectExchange(EXCHANGE, true, false);
	}

	@Bean
	public Queue queue() {
		// 队列不自动删除
		return new Queue(QUEUE, true, false, false);
	}

	@Bean
	public Binding binding() {
		return BindingBuilder.bind(queue()).to(defaultExchange()).with(ROUTINGKEY);
	}

//	@Bean
//	public SimpleMessageListenerContainer messageContainer() {
//		SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory());
//		container.setQueues(queue());
//		container.setExposeListenerChannel(true);
//		container.setMaxConcurrentConsumers(1);
//		container.setConcurrentConsumers(1);
//		container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
//		container.setMessageListener(new ChannelAwareMessageListener() {
//			@Override
//			public void onMessage(Message message, com.rabbitmq.client.Channel channel) throws Exception {
//				byte[] body = message.getBody();
//				System.out.println("消费端接收到消息 : " + new String(body));
//				channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//			}
//			
//		});
//		return container;
//	}

}
